package com.example.mytest;

import android.annotation.SuppressLint;
import android.media.MediaRecorder;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.util.SparseArray;
import android.widget.ListView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Hashtable;

public class HandlerTest {
    private static final String TAG = "dddddd";
    SparseArray spareArray = new SparseArray<>();
    //        PowerManager.WakeLock wakeLock = new PowerManager.WakeLock();
//        val looper = mHandler?.looper
//        prepare() //Only one Looper may be created per thread //主线程调用prepare（）方法报错。


    private void sendMsg() {

//        Handler mHandler = new Handler() {
//            @Override
//            public void handleMessage(@NonNull Message msg) {
//                super.handleMessage(msg);
//                mHandler.removeMessages(1);
//                Log.e(TAG, "getMessage from handler==" + msg.what);
//            }
//        };
    }

    public static void test(TextView mSendMsg) {
        HashMap<String,Integer> hashmap = new HashMap<String,Integer>();
        Hashtable<Integer, Float> hashTable = new Hashtable<Integer, Float>();
        ListView listView = new ListView(mSendMsg.getContext());
        MediaRecorder mediaRecorder = new MediaRecorder();
        mediaRecorder.setVideoFrameRate(4);//帧率
        mediaRecorder.setCaptureRate(20);//

//        new Thread() {
//            @Override
//            public void run() {
//                super.run();
//                Looper.prepare();//子线程想要创建Handler对象,主动调用Looper.prepare()方法.
//                Handler mHandler = new Handler();
//                try {
//                    Thread.sleep(5000);
//                    mSendMsg.setText("childrenThread show msg"+Thread.currentThread().getName());
//                    Log.e("TAG", "ddddddeee children create mHandler="+Thread.currentThread().getName()+"looper="+mHandler.getLooper());
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        }.start();

        new Thread() {
            @SuppressLint("HandlerLeak")
            @Override
            public void run() {
                super.run();
                Log.e("eeeeeee","thread.id = " + Thread.currentThread().getId());
                Looper.prepare();
                Handler handler = new Handler(Looper.getMainLooper()) {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        Log.e("eeeeeee","thread.id = " + Thread.currentThread().getId() + ", what = " + msg.what);
                    }
                };
                Log.e("eeeeeee","loop.之前");  // 执行了
                Looper.loop();  // 执行了
                Log.e("eeeeeee","loop.之后");  // 无法执行
            }
        }.start();

    }
}
